package com.example.seckilldemo.vo;

import com.example.seckilldemo.utils.ValidatorUtils;
import com.example.seckilldemo.validator.isMobile;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author Wangbaba
 * @Date 2024/3/18 18:11
 * @Version 1.0
 * 手机号码验证规则 自定义注解
 */
public class isMobileValidator implements ConstraintValidator<isMobile,String> {

    private boolean required=false;

    @Override
    public void initialize(isMobile constraintAnnotation) {
       required= constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (required){
            return ValidatorUtils.isMobile(s);
        }else {
            if (StringUtils.isEmpty(s)){
                return true;
            }else {
                return ValidatorUtils.isMobile(s);
            }
        }
    }
}
